package com.zlfund.cat.zjgsmk.util.filepick;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.MediaStore;

import com.zlfund.cat.zjgsmk.ui.base.ZApplication;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenj on 2017/9/26 0026.
 */

public class FilePick {

    private OnFileListChangeListener mListener;
    private List<LocalMediaImage> mImageList;
    private Map<String, LocalMediaImage> mImageMap;
    private ArrayList<LocalImageDirectory> mDirectories;

    public FilePick() {
        mImageList = new ArrayList<>();
        mImageMap = new HashMap<>();
        mDirectories = new ArrayList<>();
    }

    public void setOnFileListChangedListener(OnFileListChangeListener l) {
        mListener = l;
    }

    public void filePick() {
        FilePickRunnable runnable = new FilePickRunnable();
        new Thread(runnable).start();
    }

    private class FilePickRunnable implements Runnable {

        @Override
        public void run() {
            if (mListener != null){
                mListener.pickStart();
            }
            findThumbFile();
        }

        private void findThumbFile() {
            ContentResolver resolver = ZApplication.mContext.getContentResolver();
            Cursor thumbCursor = resolver.query(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID},
                    null,
                    null,
                    MediaStore.Images.Thumbnails.DATA
            );
            Cursor imageCursor = resolver.query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID},
                    null, null,
                    MediaStore.Images.Media.DATA
            );
            String directoryPath = null;
            if (imageCursor != null) {
                while (imageCursor.moveToNext()){
                    String path = imageCursor.getString(0);
                    String id = imageCursor.getString(1);
                    File file = new File(path);
                    String parentPath = file.getParent();
                    LocalMediaImage bean = new LocalMediaImage(null, path, id);
                    mImageList.add(bean);
                    mImageMap.put(id, bean);
                    if (directoryPath == null || !directoryPath.equals(parentPath)){
                        directoryPath = parentPath;
                        LocalImageDirectory directory = new LocalImageDirectory(file.getParentFile().getName(), parentPath);
                        directory.addImagePath(path);
                        mDirectories.add(directory);
                    }else {
                        LocalImageDirectory directory = mDirectories.get(mDirectories.size() - 1);
                        directory.addImagePath(path);
                    }
                }
                imageCursor.close();
            }
            if (thumbCursor != null) {
                while (thumbCursor.moveToNext()){
                    String path = thumbCursor.getString(0);
                    String id = thumbCursor.getString(1);
                    if (new File(path).exists()){
                        if (mImageMap.get(id) != null){
                            mImageMap.get(id).setThumbPath(path);
                        }
                    }
                }
                thumbCursor.close();
            }
            if (mListener != null) {
                mListener.fileListChanged(mImageList);
                mListener.pickFinish();
            }
        }
    }

    public ArrayList<LocalImageDirectory> getDirectories() {
        return mDirectories;
    }

    public interface OnFileListChangeListener {
        void pickStart();

        void fileListChanged(List<LocalMediaImage> list);

        void pickFinish();
    }

}
